Repository navigation
perf(profiler): bound shared zstd encoder concurrency and memory - #5985
Open
AliDatadog wants to merge 2 commits into
Open
AliDatadog wants to merge 2 commits into
AliDatadog wants to merge 2 commits into
Conversation
Configure newSharedZstdEncoder with WithEncoderConcurrency(1), WithLowerEncoderMem(true), and WithWindowSize(1<<20) so the retained encoder does not permanently hold GOMAXPROCS x 8 MiB history buffers in live heap. Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Config Audit |
Contributor
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: fc6182d | Docs | View more details | Give us feedback! |
…on ratio Keep only WithEncoderConcurrency(1) and WithLowerEncoderMem(true) so multi-MiB profiles keep identical compressed size while still cutting retained live heap by ~46% (~8.7 MiB/process). Co-Authored-By: Claude <noreply@anthropic.com>
AliDatadog
marked this pull request as ready for review
November 25, 2026 16:33
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Contributor
There was a problem hiding this comment.
More details
The static reviewer’s focused compression, configuration, error-handling, and retained-heap checks support the encoder option change, with no reportable defect identified. No human PR discussion was present.
🤖 Bits Code Review · Commit fc6182d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In production Go live-heap profiling of the internal Datadog Agent fleet (
service:datadog-agent, 4-hourheap-live-sizeprofile, 126.6 MiB average live heap), the profiler's retained zstd encoder history buffers account for 11.12% of total Go live heap (~14+ MiB per process).Every Go service running Datadog Continuous Profiler shares a single long-lived zstd encoder to compress outgoing profiles once per minute, with access already serialized by a size-1 semaphore. However, the encoder is currently created with only the compression level option. By default,
klauspost/compress/zstdspawnsGOMAXPROCSconcurrent compression workers and retains history buffers for each of them — pinning ~18–19 MiB of permanent live Go heap per process even on services whose profiles average only ~6 KiB compressed (~25 KiB uncompressed).Solution
Configure the shared zstd encoder with:
The default 8 MiB compression window is intentionally kept unchanged so compression ratio remains byte-for-byte identical even on multi-megabyte profiles.
Trade-off Analysis & Why This Is Safe in Production
1 KiBup to10 MiB(~2× the largest profile uploaded across all internal Go services in a 4-hour window).+0.6to+4.3 msbecause work runs on 1 core instead of spreading across multiple cores in parallel. Total CPU time is equal or lower, recompression allocations drop −37%, and on prod-average 25 KiB profiles wall time is actually faster (0.12 ms → 0.07 ms).60,000 msdefault period), completely off the request path and before the10 sHTTP upload timeout. Even at 2× our 4-hour fleet-wide peak payload (10 MiB),+4.3 msis 0.007% of the 60-second window. Running on 1 goroutine also stops the profiler from bursting acrossGOMAXPROCScores once per minute and competing with request handlers.1 MiB(which saved another ~7 MiB on small profiles), but on 4–10 MiB profiles it inflated compressed size by ~2× (166 → 318 KBand347 → 728 KB). We dropped that option to guarantee zero egress regression.Production Payload Size Context
Across all internal production Go services (
datadog.profiling.go.uploaded_profile_bytes, 4-hour window):6.1 KiB(~25 KiB uncompressed)632 KiB(~2–3 MiB uncompressed)1.38 MiB(~5–6 MiB uncompressed)Full Benchmark Matrix (30/30 Cells Verified)
Measured on Apple M4 Max (arm64) across all 4 supported zstd levels, 5 payload sizes (
1 KiBto10 MiB= ~2× prod peak, derived from the repository's real heap profile fixture), andGOMAXPROCS=1, 4, 16:Default Level Across
GOMAXPROCS(1,4,16)GOMAXPROCSAll 4 Compression Levels (
GOMAXPROCS=16)Recompression Benchmark (
gzip-1 -> zstd, 311 KiB fixture)1.57 → 2.29 ms/op(+0.7 ms once per minute ≈ ~0.001% CPU), allocations −37% (109 → 68.8 KiB/op,141 → 132 allocs/op).How to Reproduce Locally
All 67 CI checks (and local
-racetests acrossGOMAXPROCS=1, 2, 4, 16) pass with zero test modifications required for byte-for-byte zstd level verification.🤖 Generated with Claude Code